/** 其他场景问答逻辑 */

/*
其他场景问答逻辑数据结构(采用先添加后编辑逻辑)

数据id：aid = 1，
问题：problem = '',
答案：answer = "",
会话ID：drawinglist_id = 2,
音频地址：audio_url = '',
gpt数据id: msg_id = '',
*/

import { ChatUtils, ChatRequestTest } from '@/common/gpt/chatUtils';
import { XCXChatUtils } from "@/common/gpt/xcxChatUtils.js";
import { minganciBiaoshu } from '@/common/gpt/type';
import { HOME_MODULE_TYPE } from '@/const';

/** 其他场景问答逻辑逻辑 */
export const otherSceneMixin = {
	data() {
		return {
			osSendMsgGen: null,
		}
	},
	onLoad() {
		this.osSendMsgGen = this.createOsTiwenHandler();
	},
	methods: {
		/** 创建其他场景问答逻辑提问流程 */
		* createOsTiwenHandler() {
			yield this.osWentiSave();
			yield this.osStartTiwen();
			yield this.osDaanSave();
		},
		// /**
		//  * 切换当前会话 
		//  * @property {String} id 会话ID 
		//  * */
		// changeCSession(id = '') {
		// 	this.cSessionId = id;
		// 	this.getMsgList();
		// },
		// /** 无会话回调 */
		// notSession() {
		// 	this.msgList = [];
		// 	this.pageLoading = false;
		// 	this.empty = true;
		// 	this.page = 1;
		// },
		/** 万能提问失败处理 */
		osTiwenFailFn() {
			this.sendLoading = false;
			this.osSendMsgGen = this.createOsTiwenHandler();
		},
		/** 发起提问 */
		async osStartTiwen() {
			var parentMessageId;
			if (this.fastTiwenCMsgId) parentMessageId = this.fastTiwenCMsgId;
			else if (this.msgList.length > 2)
				parentMessageId = this.msgList[this.msgList.length - 2]?.msg_id;
			this.fastTiwenCMsgId = "";
			const prompt = this.problem;
			this.problem = "";
			this.renderMsg = "";

			// #ifndef MP-WEIXIN
			this.gptProblem = prompt + "&&&" + (parentMessageId || "");
			// #endif

			// #ifdef MP-WEIXIN
			try {
				const res = await XCXChatUtils.request({
					options: { parentMessageId },
					prompt,
					type: Number(this.$store.state.cHomeModule == HOME_MODULE_TYPE.GPT4)
				});
				this.msgList[this.msgList.length - 1].msg_id = res.id;
				this.msgList[this.msgList.length - 1].answer = res.result;
				this.gptLastBackFn(res);
			} catch (e) { this.gptErrBackFn(e) }
			// #endif

		},
		/** 存储答案 */
		async osDaanSave() {
			try {
				const { aid, answer, msg_id, is_end, } = this.msgList[this.msgList.length - 1];
				const res = await this.$post('65221ff060420', {
					record_id: aid,
					content: answer,
					msg_id: msg_id,
					is_end: is_end,
					send_type: this.cGptVersion,
				})
				await this.getMsgList();
			} catch (e) { this.$msg((e || {}).msg || '存储答案失败', 'e') }
			this.osTiwenFailFn();
		},
		/** 问题存储 */
		async osWentiSave() {
			try {
				const res = await this.$post('65221f316dae0', {
					content: this.problem,
					scenelist_id: this.sceneId,
					send_type: this.cGptVersion,
                    content_new: this.tempGptProblemJson || "",
				});
                this.tempGptProblemJson = "";
				this.resetGetUserInfo();
				await this.getMsgList();
				setTimeout(() => { this.osSendMsgGen.next(); }, 0)
			} catch (e) {
				this.$msg((e || {}).msg || '问题存储失败', 'e');
				this.osTiwenFailFn();
			}
		},
		/** 停止提问 */
		osStopTiwenFn() { ChatRequestTest.stopRequest() },
		/** 获取问答列表 */
		async osGetMsgList() {
			try {
				const res = await this.$post('652220a0deec7', {
					scenelist_id: this.sceneId,
					page: this.page,
					send_type: this.cGptVersion,
				});
				const _list = (res.data.data || []).map(item => {
					return {
						...item,
						// 自定义参数
						createAudioLoading: false, // 是否生成音频
						isPlayingAudio: false, // 是否播放当前音频
					}
				});
				var _num;
				if (this.page == 1) {
					_num = 1;
					this.msgList = _list;
					this.viewLastMsg(_num);
				} else {
					_num = this.msgList.length;
					this.msgList = [..._list, ...this.msgList];
					this.scrollToBeforeMore();
				}
				this.isHaveMore = res.data.total > this.msgList.length;
			} catch (e) { this.isHaveMore = false; }
			this.empty = !this.pageLoading && !this.msgList;
		},
	}
};